﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RFIDStockManagement.BLL;
using DevExpress.XtraEditors;
using IOReadWriter;
using RFIDStockManagement.Model.Public;
using RFIDStockManagement.Common.Public.Enums;
using RFIDStockManagement.Common.Public;

namespace RFIDStockManagement.Client
{
    public partial class StockCheckForm : XtraForm, IStockContainer
    {
        private RfidSpeedwayReader m_RfidReader = null;
        private RfidWriter m_RfidWriter = null;
        bool isRealCloseForm;
        OutStockCheckManger _checker;
        SoundAlert alerter;

        DataTable infosTable;

        public StockCheckForm()
        {
            InitializeComponent();
            isRealCloseForm = false;
            this.FormClosing += new FormClosingEventHandler(StockCheckForm_FormClosing);
            this.SizeChanged += new EventHandler(StockCheckForm_SizeChanged);
            this.gridView1.CustomDrawRowIndicator += new DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventHandler(view_CustomDrawRowIndicator);

            _checker = new OutStockCheckManger();
            infosTable = new DataTable();
            infosTable.Columns.Add("FInfo", typeof(string));
            alerter = new SoundAlert();
            alerter.SoundPath = Application.StartupPath + "\\alert.wav";
            //alerter.Alert();
            gridControl1.DataSource = infosTable;
        }

        

        private void StockCheckForm_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide();
                this.notifyIcon1.Visible = true;
            }
            else
            {
                this.notifyIcon1.Visible = false;
            }
        }

        private void StockCheckForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                if (!isRealCloseForm)
                {
                    notifyIcon1.Visible = true;
                    e.Cancel = true;
                    this.Hide();
                }
                else
                {
                    Stop(true);
                    notifyIcon1.Visible = false;
                }
                
            }
        }

        private void toolShowMainForm_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
            this.Show();
            this.Activate();
        }

        private void toolNotiExit_Click(object sender, EventArgs e)
        {
            
            isRealCloseForm = true;
            this.Close();
        }

        private void StockCheckForm_Load(object sender, EventArgs e)
        {
            if (m_RfidReader == null)
            {
                string[] readerCfg = SpeedwayPublicFunction.GetOutCheckSpeedwayCfg();
                if (readerCfg == null)
                {
                    MessageBox.Show("Speedway配置不正确", "信息提示");
                    this.Close();
                    return;
                }
                m_RfidReader = new RfidSpeedwayReader(readerCfg[0], readerCfg[1], EnumIOStockOperate.OutCheck);
            }
            m_RfidWriter = new RfidWriter(this, EnumIOStockOperate.OutCheck);
            Start();
        }

        /// <summary>
        /// 添加信息到gridview
        /// </summary>
        /// <param name="infos"></param>
        private void AddInfos(List<string> infos)
        {
            DataRow newrow;
            gridView1.BeginDataUpdate();
            foreach (string x in infos)
            {
                newrow = infosTable.NewRow();
                newrow["FInfo"] = x;
                infosTable.Rows.InsertAt(newrow, 0);
                
            }
            gridView1.EndUpdate();
            
           
        }



        private void toolExit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
           
            this.Close();
        }


        private void view_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            
            if (e.Info.IsRowIndicator)
            {
                if (e.RowHandle >= 0)
                {
                    e.Info.DisplayText = (e.RowHandle+1).ToString();
                }
                
            }
        }

        private void toolDeleteSels_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
           
        }

        private void toolClear_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.BeginDataUpdate();
            infosTable.Clear();
            gridView1.EndDataUpdate();
            alerter.StopAlert();
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Show();
            this.Activate();
        }

        private void mnuExit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            
            this.isRealCloseForm = true;
            this.Close();
        }

        #region IStockContainer Member
        private DateTime _datePoint = DateTime.Now;
        public bool IsShowed { get; set; }
        public void ShowRfidData(List<RfidDataInfo> lstResult)
        {
            List<string> lstRfid = new List<string>();
            foreach (RfidDataInfo item in lstResult)
            {
                lstRfid.Add(item.Rfid);
            }
            _checker.AddRFID(lstRfid);
            //if ((DateTime.Now - _datePoint).TotalMilliseconds >= 500)
            {
                lstRfid = _checker.Check();
                _datePoint = DateTime.Now;
                AppendGridDataDelegate delegateMothod = new AppendGridDataDelegate(AppendGridDataHandler);
                this.Invoke(delegateMothod, lstRfid);
            }
        }
        private delegate void AppendGridDataDelegate(List<string> lstRfid);
        private void AppendGridDataHandler(List<string> lstResult)
        {
            try
            {
                if (lstResult != null && lstResult.Count > 0)
                {
                    this.AddInfos(lstResult);
                    alerter.Alert();
                }
            }
            finally
            {
                IsShowed = true;
            }
        }

        #endregion

        private void Start()
        {
            m_RfidReader.StartRead();
            m_RfidWriter.StartWrite();
        }
        public void Stop(bool disConnect)
        {
            if (m_RfidReader != null)
            {
                m_RfidReader.StopRead(disConnect);
            }
            if (m_RfidWriter != null)
            {
                m_RfidWriter.StopWrite();
            }
        }

    }
}
